package com.hdaccp.ch04

import org.apache.spark.sql.SparkSession

/**
  * spark sql的入门程序
  */
object Demo1 {
  def main(args: Array[String]): Unit = {
    //1.得到一个SparkSession对象
    val spark = SparkSession.builder()
      .master("local[2]")
      .appName("ch04Demo1App")
      .getOrCreate()
   //2.引入隐式转换
    import spark.implicits._

    //3.读一个json文件
    val dataFrame = spark.read.json("F:/resources/people.json")

    dataFrame.show()
    println("----------------------------") ;
    dataFrame.printSchema()
    println("----------------------------") ;
    dataFrame.select("name").show()
    println("----------------------------") ;
    dataFrame.select($"name",$"age" + 1).show()
    println("----------------------------") ;
    dataFrame.select($"name".as("姓名"),($"age" + 1).as("年龄")).show()
    println("----------------------------") ;
    dataFrame.filter($"age">19).show()
    println("----------------------------") ;
    dataFrame.groupBy("age").count().show()
    spark.stop()
  }
}
